let PJYSDK = require('../sdk/PJYSDK')
let DIALOG = require('../sdk/DIALOG')

module.exports = function (data) {
  // console.log(data)
  let pjysdk = new PJYSDK(data.appKey, data.appSecret)

  //监听试用用户
  Object.defineProperty(pjysdk, 'is_trial',{
    get : function () {
      return is_trial
    },
    set : function (value) {
      console.log('set:', value)
      ui.run(() => {
        if (value) {
          ui['trialText'].setText('(试用)')
        } else {
          
        }
      })
    }
  })

  //监听到期时间
  Object.defineProperty(pjysdk, 'endTime',{
    get : function () {
      return endTime
    },
    set : function (value) {
      console.log('set:', value)
      ui.run(() => {
        ui['endTimeText'].setText(value)
      })
    }
  })
  
  //监听是否登陆成功
  Object.defineProperty(pjysdk,'statCode',{
    get : function () {
      return statCode
    },
    set : function (value) {
      statCode = value
      console.log('set:', value)
      ui.run(() => {
        if (value == 0) {
          ui['user_login'].setVisibility(8)
          ui['user_my'].setVisibility(0)
        } else {
          ui['user_login'].setVisibility(0)
          ui['user_my'].setVisibility(8)
          //删除本地存储的卡密
          
        }
      })
    }
  })

  pjysdk._protocol = "https"
  pjysdk.debug = true
  
  let xml = (
    <frame margin='8dp'>
      <card
        cardBackgroundColor="#ffffff"
        cardCornerRadius='10dp'
        cardElevation='2dp'
        foreground="?selectableItemBackground"
        w='*'
        h='auto'
      >
        <vertical>
          <horizontal w='*' bg={this.app.color} gravity='center' padding='5dp'>
            <text textColor={this.app.font_color} text='用户中心' textSize='16sp'></text>
          </horizontal>
          <frame padding='5dp' id='user_body'>
            {user_body.call(this)}
          </frame>
        </vertical>
      </card>
    </frame>
  )



  function user_body () {
    let xml = (
      <frame>
        <horizontal visibility='gone' w='*' id='user_login' >
          <button textColor={this.app.icon_color} id='activation' style='Widget.AppCompat.Button.Borderless' layout_weight='1' text='激活'></button>
          <button textColor={this.app.icon_color} id='trial' style='Widget.AppCompat.Button.Borderless' layout_weight='1' text='试用'></button>
        </horizontal>
        
        <vertical visibility='gone' w='*' id='user_my'>

          <horizontal gravity='center_vertical'>
            <text textStyle='bold' lines='1' textSize='18sp' text={device.brand +'_'+ device.model}></text>
            
          </horizontal>

          <text text=' ' w='*' h='2dp' margin='0 5dp 0 5dp' bg='#b2b2b2'></text>

          {/* <horizontal gravity='center_vertical'>
            <img w='25dp' h='25dp' tint={this.app.color} src='@drawable/ic_credit_card_black_48dp'></img>
            <text text='设备ID：'></text>
            <text text={pjysdk._device_id}></text>

          </horizontal> */}

          <horizontal gravity='center_vertical'>
            {/* <img w='25dp' h='25dp' tint={this.app.color} src='@drawable/ic_access_time_black_48dp'></img> */}

            <text text='到期时间：'></text>
            <text id='endTimeText'></text>
            <text id='trialText'></text>
          </horizontal>

          {/* 按钮控件组 */}
          <horizontal visibility='visible' w='*'>
            <button textColor={this.app.icon_color} id='renew' style='Widget.AppCompat.Button.Borderless' layout_weight='1' text='续卡'></button>
            <button textColor={this.app.icon_color} id='unbind' style='Widget.AppCompat.Button.Borderless' layout_weight='1' text='解绑'></button>

          </horizontal>

        </vertical>
      </frame>
    )
    //d
    console.log('我的ID', pjysdk.getDeviceID())
    //设置本地数据库
    state = this.state
    if (!state.contains('card_passward') || state.get('card_passward') == '') {
      ui.post(() => {
        ui.user_login.setVisibility(0)
        // console.log('卡密'+this.state)
        ui.activation.on('click', () => {
          // toastLog('激活')
          threads.start(function(){
  
            ui.run(() => {
              DIALOG.input('请输入卡密', '', true, (c) => {
                console.log('输入的卡密为-->>  '+c)
                if (c == '') {
                  toastLog('请输入卡密')
                  // gaKDovxrXZOvdewTRovl
                } else {
                  pjysdk.SetCard(c)
                  let pjyres = pjysdk.CardLogin()
                  pjysdk.statCode = pjyres.code
  
                  if (pjysdk.statCode == 0) {
                    console.log(pjyres.message)
                    // ui['user_login'].setVisibility(8)
                    //更新卡密到期时间
                    pjysdk.endTime = pjyres.result.expires
                    //保存卡密到本地存储
                    state.put('card_passward', c)
                    // console.log('卡密', state.get('card_passward'))
                  } else {
                    toastLog('登录失败：'+pjyres.message)
                  }
  
                }
              })
            })
  
          })
        })

        ui.trial.on('click', () => {
          // toastLog('试用')
          threads.start(function(){
  
            ui.run(() => {
              let pjyres = pjysdk.TrialLogin()
              // let pjyres = pjysdk.TrialLogin()
              // console.log('错误：【'+JSON.stringify(pjyres)+'】')
              pjysdk.statCode = pjyres.code

              if (pjysdk.statCode == 0) {
                console.log(pjyres.message)
                pjysdk.endTime = pjyres.result.expires
                
              } else {
                toastLog('登录失败：'+pjyres.message)
                
              }
            })
  
          })
        })
  
      })
    } else {
      //开始自动调取卡密登陆
      let c = state.get('card_passward')
      ui.post(() => {
        // ui.user_body.setVisibility(0)
        pjysdk.SetCard(c)
        let pjyres = pjysdk.CardLogin()
        pjysdk.statCode = pjyres.code

        if (pjysdk.statCode == 0) {
          console.log(pjyres.message)
          // ui['user_login'].setVisibility(8)
          //更新卡密到期时间
          pjysdk.endTime = pjyres.result.expires
          //保存卡密到本地存储
          state.put('card_passward', c)
          // console.log('卡密', state.get('card_passward'))
        } else {
          toastLog('登录失败：'+pjyres.message)
          state.remove('card_passward')
        }
      })
    }
    return xml
  }

  return xml

}